﻿////////////////////////////////////////////////////////////////////
// © Copyright 2007 NeoGeo New Media GmbH (http://www.neogeo.com) //
////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// resets all tiles *******************************************
// (resets the source and opacity for all tiles)
///////////////////////////////////////////////////////////////
function ClearAndReset(firstlayer, secondlayer)
{
    if(firstlayer == 'undefined') firstlayer = true;
    if(secondlayer == 'undefined') secondlayer = true;
    for(var j = 0; j < 3; j++)
    {
        for (var i = 0; i < 3; i++)
        {
            var tile = MainRootElement.findName("Tile_0_" + j + "_" + i);
            var subtile = MainRootElement.findName("Tile_1_" + j + "_" + i);
            if(firstlayer)
            {
                tile["Source"] = "";
                tile["Opacity"] = 0;
            }
            if(secondlayer)
            {
                subtile["Source"] = "";
                subtile["Opacity"] = 0;
            }
        }
    }
}

///////////////////////////////////////////////////////////////
// resets the scene *******************************************
// (resets the scene to its original state)
///////////////////////////////////////////////////////////////
function ResetScene()
{
    var sceneTranslate = MainRootElement.findName("sceneTranslate");
    var sceneScale = MainRootElement.findName("sceneScale");
    sceneTranslate.X = 0;
    sceneTranslate.Y = 0;
    sceneScale.CenterX = (OrigSizeX/2) + LeftSpacing;
    sceneScale.CenterY = (OrigSizeY/2) + TopSpacing;
    sceneScale.scaleX = 1;
    sceneScale.scaleY = 1;
    CalculateEdges();
    OverviewZoom();
}

///////////////////////////////////////////////////////////////
// main entry point for the tile logic ************************
// (checks which state of tile logic has to be processed)
///////////////////////////////////////////////////////////////
function processTileLogic()
{
    CalculateEdges();
    
    // don't process any tiles if not in an appropriate zoom-depth
    if(Zoom() >= ZoomLoad) // zoomload represents the zoom-step whereas the next level of tiles is processed before showing them
        // start the tile-logic
        LoadTiles();
    else if(Zoom() == 1)
        ClearAndReset();
}

///////////////////////////////////////////////////////////////
// main load function for tiles *******************************
// (progressive load function for the tiles)
///////////////////////////////////////////////////////////////
function LoadTiles()
{
    // size of each tile image including the zoom depth
    var parent = MainRootElement.findName("Tile_0_0_0");
    var tXSize = Math.round(parent["Width"] * Zoom());
    var tYSize = Math.round(parent["Height"] * Zoom());

    // loop through a grid and find all visible or partially visible tiles
    for(var i = 0; i < 3; i++)
    {
        for (var j = 0; j < 3; j++)
        {
            // calculate the x and y position of the tile
            var tX  = i * tXSize + parent["Canvas.Left"]*Zoom() - LeftSpacing*Zoom();
            var tY = j * tYSize + parent["Canvas.Top"]*Zoom() - TopSpacing*Zoom();
            
            // process only fully or partial visible tiles
            if(tX <= VisX + OrigSizeX && VisX <= tX + tXSize &&  tY <= VisY + OrigSizeY && VisY <= tY+75 + tYSize)
            {   
                // if the zoom-level is at the minimum process the first-level tiles
                if(Zoom() < ZoomLevel + ZoomLevel)
                {
                    var tile = MainRootElement.findName("Tile_0_" + j + "_" + i);
                    var subTile = MainRootElement.findName("Tile_1_" + j + "_" + i);
                    var tileFile = image_dir + window[image_name + "_" + i + "_" + j][0];
                    subTile["Source"] = "";
                    subTile["Opacity"] = 0.0;
                    if(tile["Source"] != tileFile)
                    {
                        tile["Opacity"] = 0.0; 
                        tile["Source"] = tileFile; 
                        FadeInTileStart("Tile_0_" + j + "_" + i);
                    }                        
                }
                // if the zoom-level is greater than first level calculate deeper tile-logic
                else
                {
                    // calculate the visible subtiles of the currently visible tiles
                    VisibleSubTiles(i, j);
                }
            }
            // if the tile is not within the viewport then ignore it
        }
    }
}

///////////////////////////////////////////////////////////////
// sub load function for tiles *******************************
// (progressive load function for the subtiles if visibile that is)
///////////////////////////////////////////////////////////////
function VisibleSubTiles(xTile, yTile)
{
     // size of each tile image including the zoom depth
    var parent = MainRootElement.findName("Tile_0_" + yTile + "_" + xTile);
    var tXSize = Math.round(parent["Width"]/3 * Zoom());
    var tYSize = Math.round(parent["Height"]/3 * Zoom());
    
    for(var i = 0; i < 3; i++)
    {
        for(var j = 0; j < 3; j++)
        {   
            var tX  = i * tXSize + parent["Canvas.Left"]*Zoom() - LeftSpacing/3;
            var tY = j * tYSize + parent["Canvas.Top"]*Zoom() - TopSpacing/3;
            
            if(tX <= VisX + OrigSizeX && VisX <= tX + tXSize &&  tY <= VisY + OrigSizeY && VisY <= tY+75 + tYSize)
            {   
                var parent = MainRootElement.findName("Tile_0_" + yTile + "_" + xTile);
                var tile = MainRootElement.findName("Tile_1_" + j + "_" + i);
                var tileFile = image_dir + window[image_name + "_" + xTile + "_" + yTile][1][j+i*3];    
                if(tile["Source"] != tileFile)
                {
                    tile["Opacity"] = 0.0;
                    tile["Source"] = tileFile;
                    tile["Width"] = parent["Width"]/3;
                    tile["Height"] = parent["Height"]/3;
                    tile["Canvas.Left"] = parent["Canvas.Left"] + i * tile["Width"];
                    tile["Canvas.Top"] = parent["Canvas.Top"] + j * tile["Height"];                    
                    
                    FadeInTileStart("Tile_1_" + j + "_" + i);                    
                }
            }
        }
    }
}

///////////////////////////////////////////////////////////////
// reorder function *******************************************
// (function for reordering the tiles according to the original main image)
///////////////////////////////////////////////////////////////
function ReArrangeTiles()
{
    // first reset to original values
    for(var i = 0; i < 3; i++)
    {
        for(var j = 0; j < 3; j++)
        {
            var oldTile = MainRootElement.findName("Tile_0_" + j + "_" + i);
            var oldSubTile = MainRootElement.findName("Tile_1_" + j + "_" + i);
            oldTile["Width"] = Math.round(OrigSizeX / 3);
            oldTile["Height"] = Math.round(OrigSizeY / 3);
            oldTile["Canvas.Top"] = j * oldTile["Height"] + 10;
            oldTile["Canvas.Left"] = i * oldTile["Width"] + 10;
            oldTile["Source"] = "";
            
            oldSubTile["Width"] = Math.round(OrigSizeX / 3);
            oldSubTile["Height"] = Math.round(OrigSizeY / 3);
            oldSubTile["Canvas.Top"] = j * oldTile["Height"] + 10;
            oldSubTile["Canvas.Left"] = i * oldTile["Width"] + 10;
            oldSubTile["Source"] = "";
        }
    }
    
    // calculate wether the image is portrait or landscape
    if(tile_height >= tile_width)
    {
        // portrait image
        var factor = (tile_height * 3) / OrigSizeY;
    }
    else
    {
        // landscape image
        var factor = (tile_width * 3) / OrigSizeX;
    }
    
    // calculate the new tile-values
    var newHeight = tile_height / factor;
    var newWidth = tile_width / factor;
    var newTop = (OrigSizeY / 3) - newHeight;
    var newLeft = (OrigSizeX / 3) - newWidth;
    
    // loop through the tile grid and rearrange the tiles
    for(var x = 0; x < 3; x++)
    {
        for(var y = 0; y < 3; y++)
        {            
            var tile = MainRootElement.findName("Tile_0_" + x + "_" + y);            
            /*var rectTile = MainRootElement.findName("Rect_0_" + x + "_" + y);
            rectTile["Height"] = newHeight;
            rectTile["Width"] = newWidth;*/
            
            tile["Height"] = newHeight;
            tile["Width"] = newWidth;
            
            if(x == 0)
            {
                tile["Canvas.Top"] += Math.round(2*newTop - newTop/2);
                //rectTile["Canvas.Top"] = tile["Canvas.Top"];
            }
            else if(x == 1)
            {
                tile["Canvas.Top"] += Math.round(newTop/2);
                //rectTile["Canvas.Top"] = tile["Canvas.Top"];
            }
            else if(x == 2)
            {
                tile["Canvas.Top"] -= Math.round(newTop/2)+1; 
                //rectTile["Canvas.Top"] = tile["Canvas.Top"];
            }
                
            if(y == 0)
            {
                tile["Canvas.Left"] += Math.round(2*newLeft - newLeft/2);
                //rectTile["Canvas.Left"] = tile["Canvas.Left"];
            }
            else if(y == 1)
            {
                tile["Canvas.Left"] += Math.round(newLeft/2);
                //rectTile["Canvas.Left"] = tile["Canvas.Left"];
            }
            else if(y == 2)
            {
                tile["Canvas.Left"] -= Math.round(newLeft/2);
                //rectTile["Canvas.Left"] = tile["Canvas.Left"];
            }
            
        }
    }
}